//
// Created by Felix on 24-4-23.
//

#include "oatpp/network/Server.hpp"
#include "oatpp-swagger/Controller.hpp"

#include "AppComponent.hpp"

#include "controller/IndexController.hpp"
#include "controller/AuthController.hpp"
#include "controller/UserController.hpp"
#include "controller/CameraController.hpp"

void run() {
    AppComponent components;

    OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
    OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);
    OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
    oatpp::network::Server server(connectionProvider, connectionHandler);

    // controller 路由注册
    oatpp::web::server::api::Endpoints docEndpoints;
    docEndpoints.append(
            router->addController(IndexController::createShared())->getEndpoints()
    );
    docEndpoints.append(
            router->addController(AuthController::createShared())->getEndpoints()
    );
    docEndpoints.append(
            router->addController(UserController::createShared())->getEndpoints()
    );
    docEndpoints.append(
            router->addController(CameraController::createShared())->getEndpoints()
    );
    router->addController(oatpp::swagger::Controller::createShared(docEndpoints));

    OATPP_LOGI("MyApp", "Server running on port %s", connectionProvider->getProperty("port").getData())

    /* Run server */
    server.run();

    OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider);
    dbConnectionProvider->stop();
}

int main() {
    oatpp::base::Environment::init();

    run();

    oatpp::base::Environment::destroy();

    return 0;
}
